var jQT = new $.jQTouch({
    icon: '/images/icon.png',
    addGlossToIcon: true,
    startupScreen: '/images/startup.png',
    statusBar: 'black',
    preloadImages: ['/jqt/themes/jqt/img/button.png',
                    '/jqt/themes/jqt/img/back_button.png',
                    '/jqt/themes/jqt/img/back_button_clicked.png',
                    '/images/plus_circle.png']
});

function updateCurrentSong(data) {
    var html;
    if(data.message) {
    	html = $('<span>').html(data.message).html();
    } else {
    	html = $('<span>').html(data.song.title.toUpperCase() + '<br/>from ' + data.song.album.toUpperCase() + '<br/>by ' + data.song.artist.toUpperCase()).html();
    }
    
    if ($('#currentSong').html() != html) {
        $('#currentSong').html(html);
    }
}

function updateSongList(data) {
	$('#songs-in-album').html('');
	$('<li/>').addClass('sep').text(data.songs[0].album).appendTo('#songs-in-album');
	$.each($(data.songs), function(index, value) {
		var songId = value.id.replace(/=/g, /-/);
		$('<li/>').addClass('songName tobeselected').attr('id', songId).text(value.title).click(function() {
            $.ajax({
                type: 'POST',
                url: '/queue/add/' + value.id,
                success: function(data) { $('#' + songId).unbind('click').removeClass('tobeselected').addClass('selected'); },
                dataType: 'json'
              });
		}).appendTo('#songs-in-album');
	});
}

function showError() {
    $('#currentSong').text('Error getting data from server');
}

$.timer(20000, function (timer) {
    $.ajax({
          type: 'POST',
          url: '/stats/currentSong',
          success: function(data) { updateCurrentSong(data); },
          error: function(request, status, errorThrown) {showError();},
          dataType: 'json'
    });
});

$(function() {
    $.each($('li.playerSelect'), function(index, value) {
       $(value).click(function() {
           var selection = $(this).attr('rel');
           $('#album-name-results').text('');
           $('#currentSong').text('Player selection unsuccessful. Please go back and retry.');
           $.ajax({
               type: 'POST',
               url: '/player/' + selection,
               success: function(data) { 
                               $('#playerControls div.toolbar h1').text(selection);
                               updateCurrentSong(data);
                               jQT.goTo('#playerControls', 'cube');
                           },
               error: function(request, status, errorThrown) {showError();},
               dataType: 'json'
           });                        
       });
    });

    $.each($('#playback img.control'), function(index, value) {
        $(value).click(function() {
        	$(value).addClass('clicked');
            $.ajax({
              type: 'POST',
              url: '/playback/' + $(this).attr('rel'),
              success: function(data) { updateCurrentSong(data); $(value).removeClass('clicked'); },
              error: function(request, status, errorThrown) {showError();},
              dataType: 'json'
            });
        });
    });
    
    $.each($('#volume img.control'), function(index, value) {
        $(value).click(function() {
        	$(value).addClass('clicked');
            $.ajax({
              type: 'POST',
              url: '/volume/' + $(this).attr('rel'),
              error: function(request, status, errorThrown) {showError(); },
              success: function(data) { $(value).removeClass('clicked'); },
              dataType: 'json'
            });
        });
    });
    
    $('#album-name-text').change(function() {
    	var text = $.trim($(this).val());
    	if (text.length < 2) return;
    	$.post('/albums/'+text, function(data) {
    		  $('#album-name-results').html(data);
    		  $('#album-name-text').val('').blur();
    		  $.each($('#album-name-results li.albumName'), function(index, value) {
    			  $(value).click(function() {
    		            $.ajax({
    		                type: 'POST',
    		                url: '/songs/' + $(this).attr('rel'),
    		                dataType: 'json',
    		                success: function(data) { updateSongList(data); jQT.goTo('#songList', 'cube'); }
    		              });
    			  });
    		  });
    	});
    });
});
